<?php

class EmbeddedPersonalMemberDataForm extends PersonalMemberDataForm
{
  
  protected static $embeddedAddressesKey = 'Addresses';
  protected static $embeddedPhoneNumbersKey = 'PhoneNumbers';
  protected static $embeddedBankAccountKey = 'BankAccount';

  public function configure ()
  {
    parent::configure();
    
    // Remove fields
    unset($this['member_id'], $this['created_at'], $this['updated_at'], $this['deleted_at'], $this['created_by'], $this['updated_by'], 
        $this['deleted_by'], $this['version']);
    unset($this['addresses_list'], $this['phone_numbers_list'], $this['bank_account_id']);
    
    // Date of birth
    $this->widgetSchema['date_of_birth'] = new sfWidgetFormDateJQueryUI(array(
      "change_month" => true, "change_year" => true)); #new sfWidgetFormJQueryDate(array('date_widget' => new WidgetFormInputDate()));//new WidgetFormDatePastPeriod ();
    

    // Addresses 
    $this->embedForm(self::$embeddedAddressesKey, 
        new DynamicAddressForm($this->getObject(), array(
          DynamicAddressForm::OPTION_POST_VALIDATOR => false)));
    
    // Phone numbers
    $this->embedForm(self::$embeddedPhoneNumbersKey, new DynamicPhoneNumberForm($this->getObject()));
    
    // Bank account
    $this->embedForm(self::$embeddedBankAccountKey, new BankAccountForm($this->getObject()
      ->getBankAccount()));
  }

  public function addPreviouslyAddedPhoneNumbers (array $taintedValues = null)
  {
    sfContext::getInstance()->getLogger()
      ->log('Adding previously added phone numbers in EmbeddedPersonalMemberDataForm...', sfLogger::DEBUG);
    sfContext::getInstance()->getLogger()
      ->log('Count tainted values for form: ' . count($taintedValues), sfLogger::DEBUG);
    sfContext::getInstance()->getLogger()
      ->log('Count tainted values for embedded dynamic form: ' . count($taintedValues[self::$embeddedPhoneNumbersKey]), sfLogger::DEBUG);
    foreach ($taintedValues[self::$embeddedPhoneNumbersKey] as $key => $phone_number)
    {
      if (! isset($this[self::$embeddedPhoneNumbersKey][$key]))
      {
        $this->addPhoneNumber($key);
      }
    }
  }

  /**
   * Add embedded phone number form
   * @param integer $num
   */
  public function addPhoneNumber ($num = null)
  {
    sfContext::getInstance()->getLogger()
      ->log('Adding phone number...', sfLogger::DEBUG);
    /**
     * @var $form DynamicPhoneNumberForm
     */
    $form = $this->getEmbeddedForm(self::$embeddedPhoneNumbersKey);
    $form->addForm(null, $num);
    $this->embedForm(self::$embeddedPhoneNumbersKey, $form);
  }

  public function updateObjectEmbeddedForms ($values, $forms = null)
  {
    if (is_array($forms))
    {
      foreach ($forms as $key => $form)
      {
        $form_values = isset($values[$key]) ? $values[$key] : array();
        if ($form instanceof EmbeddedPhoneNumberForm)
        {
          if (EmbeddedPhoneNumberForm::formValuesAreBlank($form_values))
          {
            // Delete empty phone number forms and related object
            $form = $this->getEmbeddedForm(self::$embeddedPhoneNumbersKey)
              ->unembedForm($key);
            $form->getObject()
              ->delete();
          }
        }
      }
    }
    return parent::updateObjectEmbeddedForms($values, $forms);
  }

  public function saveEmbeddedForms ($con = null, $forms = null)
  {
    // Embedded PhoneNumber objects
    $dynamic_phone_number_form = $this->getEmbeddedForm(self::$embeddedPhoneNumbersKey);
    $phone_number_forms = $dynamic_phone_number_form->getEmbeddedForms();
    foreach ($phone_number_forms as $k => $phone_number_form)
    {
      if (! $phone_number_form->getObject()
        ->isNew())
        continue;
        
      $member_phone_number = new MemberPhoneNumber();
      $member_phone_number->PersonalMemberData = $this->getObject();
      $member_phone_number->PhoneNumber = $phone_number_form->getObject();
      $member_phone_number->save();
    }
    
    // Embedded Address objects
    $dynamic_address_form = $this->getEmbeddedForm(self::$embeddedAddressesKey);
    $address_forms = $dynamic_address_form->getEmbeddedForms();
    foreach ($address_forms as $address_form)
    {
      if (! $address_form->getObject()
        ->isNew())
        continue;
        
      $member_address = new MemberAddress();
      $member_address->PersonalMemberData = $this->getObject();
      $member_address->Address = $address_form->getObject();
      $member_address->save();
    }
    
    return parent::saveEmbeddedForms($con, $forms);
  }

  public function updateObject ($values = null)
  {
    parent::updateObject($values);
  }
}